package gui;

import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class MainFrame {

	private JFrame frame;
	private JButton btnPrint;
	private JButton btnInfo;
	private JButton btnOpretmellemvareMedThread;
	private JButton btnOpretMellemVareUdenThread;
	private JButton btnClean;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					MainFrame window = new MainFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 800, 150);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		btnPrint = new JButton("Vis mellemvare af type");
		btnPrint.addActionListener(new BtnPrintActionListener());
		frame.getContentPane().setLayout(
				new FlowLayout(FlowLayout.CENTER, 5, 5));
		frame.getContentPane().add(btnPrint);

		btnInfo = new JButton("Vis mellemvare tættest på max");
		btnInfo.addActionListener(new BtnInfoActionListener());
		frame.getContentPane().add(btnInfo);

		btnOpretmellemvareMedThread = new JButton(
				"OpretMellemvare (Med Transaction)");
		btnOpretmellemvareMedThread
				.addActionListener(new BtnOpretmellemvareActionListener());

		JButton btnMinmaxTid = new JButton("Min/Max tid");
		btnMinmaxTid.addActionListener(new BtnMinmaxTidActionListener());
		frame.getContentPane().add(btnMinmaxTid);
		frame.getContentPane().add(btnOpretmellemvareMedThread);

		btnOpretMellemVareUdenThread = new JButton(
				"OpretMellemvare (Uden Transaction)");
		btnOpretMellemVareUdenThread
				.addActionListener(new BtnOpretMellemVareUdenThreadActionListener());

		btnClean = new JButton("Clean");
		btnClean.addActionListener(new BtnCleanActionListener());
		frame.getContentPane().add(btnClean);
		frame.getContentPane().add(btnOpretMellemVareUdenThread);
	}

	private class BtnPrintActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {

			dao.findMellemvarerAfType("Frugtflæsk");
		}
	}

	private class BtnInfoActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			dao.getAllMellemvarer();
		}
	}

	private class BtnOpretmellemvareActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			dao.opretMellemvareA(1);
		}
	}

	private class BtnOpretMellemVareUdenThreadActionListener implements
			ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try{
			dao.opretMellemvareE(1);
			} catch (RuntimeException e){
			JOptionPane.showMessageDialog(btnClean, e.getMessage(), "Fejl" , 0);
			}
		}
	}

	private class BtnMinmaxTidActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			dao.findMinOgMax();
		}
	}

	private class BtnCleanActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
			dao.cleanUp(3);
		}
	}
}
